package com.ellricky.baselib.util

import android.annotation.SuppressLint
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target

object Ext {
    @SuppressLint("CheckResult")
    fun ImageView.load(
        url: String, placeholder: Int, error: Int = 0,
        isCircle: Boolean = false,
        isCenterCrop: Boolean = false,
        roundRadius: Int = 0,
        isCrossFade: Boolean = false,
        isForceOriginalSize: Boolean = false
    ) {
        val options = RequestOptions().placeholder(placeholder).error(error).apply {
            if (isCenterCrop && scaleType != ImageView.ScaleType.CENTER_CROP)
                scaleType = ImageView.ScaleType.CENTER_CROP
            if (isCircle) {
                if (scaleType == ImageView.ScaleType.CENTER_CROP) {
                    transforms(CenterCrop(), CircleCrop())
                } else {
                    transform(CircleCrop())
                }
            } else if (roundRadius != 0) {
                if (scaleType == ImageView.ScaleType.CENTER_CROP) {
                    transforms(CenterCrop(), RoundedCorners(roundRadius))
                } else {
                    transform(RoundedCorners(roundRadius))
                }
            }
            if (isForceOriginalSize) {
                override(Target.SIZE_ORIGINAL)
            }
        }
        Glide.with(context).load(url)
            .apply(options)
            .apply {
                if (isCrossFade) transition(DrawableTransitionOptions.withCrossFade())
            }
            .into(this)
    }
}